package com.seventeen.mq.proxy.web.controller.proxy;

import com.seventeen.mq.proxy.common.annotation.Log;
import com.seventeen.mq.proxy.common.constant.DelFlagConstants;
import com.seventeen.mq.proxy.common.core.controller.BaseController;
import com.seventeen.mq.proxy.common.core.domain.AjaxResult;
import com.seventeen.mq.proxy.common.core.page.TableDataInfo;
import com.seventeen.mq.proxy.common.enums.BusinessType;
import com.seventeen.mq.proxy.core.domain.MqInstance;
import com.seventeen.mq.proxy.core.service.IMqInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * MQ实例
 *
 * @author seventeen
 */
@RestController
@RequestMapping(value = "/mq/instance")
public class MqInstanceController extends BaseController {

    @Autowired
    private IMqInstanceService mqInstanceService;

    @PreAuthorize("@ss.hasPermi('mq:instance:list')")
    @GetMapping("/list")
    public TableDataInfo list(MqInstance mqInstance) {
        startPage();
        List<MqInstance> list = mqInstanceService.selectList(mqInstance);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('mq:instance:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getConsumer(@PathVariable Long id) {
        return success(mqInstanceService.selectById(id));
    }

    @PreAuthorize("@ss.hasPermi('mq:instance:add')")
    @Log(title = "MQ实例管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MqInstance mqInstance) {
        mqInstance.setCreateBy(getUsername());
        mqInstanceService.add(mqInstance);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('mq:instance:edit')")
    @Log(title = "MQ实例管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody MqInstance mqInstance) {
        if (mqInstance.getId() == null) {
            return error("请求参数有误");
        }
        mqInstance.setUpdateBy(getUsername());
        mqInstanceService.update(mqInstance);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('mq:instance:remove')")
    @Log(title = "MQ实例管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        MqInstance mqInstance = new MqInstance();
        mqInstance.setId(id);
        mqInstance.setDelFlag(DelFlagConstants.DELETED);
        mqInstance.setUpdateBy(getUsername());
        mqInstanceService.update(mqInstance);
        return success();
    }

}
